package net.starwifi.duobao.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import net.starwifi.duobao.service.DBCodeService;
import net.starwifi.duobao.service.MemberService;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.*;
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 夺宝期数记录
 * Created by ruoo on 2015/11/19.
 */
@Entity
@Table(name = "duob_db_product_times")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "duob_db_product_times_sequence")
public class DBTimes extends BaseEntity {


    /** 当前进度缓存 */
    public static final String CURRENT_QUANTITY_CACHE_NAME = "currentQuantity";

    /** 夺宝商品 */
    private DBProduct dbProduct;

    /** 总份数*/
    private Integer quantity;

    /** 单份金额 */
    private BigDecimal singleAmount;

    /** 单用户限购数量*/
    private Integer purchaseQuantity;

    /** 每次叠加的份数 */
    private Integer stepNum;

    /** 已购买份数*/
    private Integer currentQuantity;

    /** 当前第几期 */
    private String timesNo;

    /** 当期开始时间 */
    private Date startDate;

    /** 当期结束时间 */
    private Date endDate;

    /** 内定中奖用户 */
    private String phone;

    /** 是否已经结束 */
    private Boolean isOver;

    /** 是否已经开奖 */
    private Boolean isLottery;

    /** 中奖用户手机号*/
    private String lotteryMobile;

    /** 中奖用户ID */
    private Long memberId;

    /** 中奖订单 */
    private DBOrder dbOrder;

    /** 中奖号码 */
    private Integer timesCode;

    /* 参与人次 */
    private Integer inCount;

    /**参考号码是多少*/
    private String referenceCode;

    /**参考数值B*/
    private String referenceCodeTwo;

    /** 期望的中奖订单号*/
    private String wantLotteryId;

    /** 是否已经晒单*/
    private Boolean isShare;

    private  List<DBCode> lotteryCode;

    @ManyToOne
    @JoinColumn(name = "db_product_id",updatable = false)
    public DBProduct getDbProduct() {
        return dbProduct;
    }

    public void setDbProduct(DBProduct dbProduct) {
        this.dbProduct = dbProduct;
    }

    @Min(0)
    @Column
    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    @Min(0)
    @Column
    public BigDecimal getSingleAmount() {
        return singleAmount;
    }

    public void setSingleAmount(BigDecimal singleAmount) {
        this.singleAmount = singleAmount;
    }

    @Min(0)
    @Column
    public Integer getPurchaseQuantity() {
        return purchaseQuantity;
    }

    public void setPurchaseQuantity(Integer purchaseQuantity) {
        this.purchaseQuantity = purchaseQuantity;
    }

    @Min(1)
    @Column
    public Integer getStepNum() {
        return stepNum;
    }

    public void setStepNum(Integer stepNum) {
        this.stepNum = stepNum;
    }

    @JsonProperty
    @Pattern(regexp = "^[0-9a-zA-Z_-]+$")
    @Length(max = 100)
    @Column(nullable = false, unique = true, length = 100)
    public String getTimesNo() {
        return timesNo;
    }

    public void setTimesNo(String timesNo) {
        this.timesNo = timesNo;
    }

    @Column
    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    @Column
    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }


    @Column
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Column
    public Integer getCurrentQuantity() {
        return currentQuantity;
    }

    public void setCurrentQuantity(Integer currentQuantity) {
        this.currentQuantity = currentQuantity;
    }

    /** 获取进度*/
    @Transient
    public Integer getProgress(){
        int prog=currentQuantity/quantity;
        return prog*100;
    }

    @ManyToOne
    @JoinColumn(name = "db_order_id")
    public DBOrder getDbOrder() {
        return dbOrder;
    }


    public void setDbOrder(DBOrder dbOrder) {
        this.dbOrder = dbOrder;
    }

    public Integer getTimesCode() {
        return timesCode;
    }

    @Min(0)
    @Column
    public void setTimesCode(Integer timesCode) {
        this.timesCode = timesCode;
    }

    public Integer getInCount() {
        return inCount;
    }
    @Min(0)
    @Column
    public void setInCount(Integer inCount) {
        this.inCount = inCount;
    }

    @Column
    public Boolean getIsOver() {
        return isOver;
    }

    public void setIsOver(Boolean isOver) {
        this.isOver = isOver;
    }

    @Column
    public Boolean getIsLottery() {
        return isLottery;
    }

    public void setIsLottery(Boolean isLottery) {
        this.isLottery = isLottery;
    }

    @Column
    public String getLotteryMobile() {
        return lotteryMobile;
    }

    public void setLotteryMobile(String lotteryMobile) {
        this.lotteryMobile = lotteryMobile;
    }

    @Column
    public Long getMemberId() {
        return memberId;
    }

    public void setMemberId(Long memberId) {
        this.memberId = memberId;
    }

    @Column
    public String getReferenceCode() {
        return referenceCode;
    }

    public void setReferenceCode(String referenceCode) {
        this.referenceCode = referenceCode;
    }

    @Column
    public String getReferenceCodeTwo() {
        return referenceCodeTwo;
    }

    public void setReferenceCodeTwo(String referenceCodeTwo) {
        this.referenceCodeTwo = referenceCodeTwo;
    }

    @Column
    public String getWantLotteryId() {
        return wantLotteryId;
    }

    public void setWantLotteryId(String wantLotteryId) {
        this.wantLotteryId = wantLotteryId;
    }

    @Column
    public Boolean getIsShare() {
        return isShare;
    }

    public void setIsShare(Boolean isShare) {
        this.isShare = isShare;
    }

    /**
     * 持久化前处理
     */
    @PrePersist
    public void prePersist() {
       this.setIsOver(Boolean.FALSE);
    }

    /** 获取中奖用户手机号*/
    @Transient
    public String getLotteryMobileInfo(){
        if(getLotteryMobile()!=null){
            String m="";
            try {
                m= getLotteryMobile().substring(0, 3) + "****" + getLotteryMobile().substring(7, getLotteryMobile().length());
            }catch (Exception e){
                m="无法获取";
            }
            return m;
        }else{
            return "无法获取";
        }

    }
    /**获取中奖用户夺宝码*/


@Transient
    public List<DBCode> getLotteryCode( ) {
        return lotteryCode;
    }

    public void setLotteryCode(List<DBCode> lotteryCode) {
        this.lotteryCode = lotteryCode;
    }
}
